package com.mrx.mxshell

import java.io.File
import java.util.*

object Main {

    @JvmStatic
    fun main(args: Array<String>) {
        val sc = Scanner(System.`in`)
        println(
            """
		
  __  ____   __   _____ _    _ ______ _      _      
 |  \/  \ \ / /  / ____| |  | |  ____| |    | |     
 | \  / |\ V /  | (___ | |__| | |__  | |    | |     
 | |\/| | > <    \___ \|  __  |  __| | |    | |     
 | |  | |/ . \   ____) | |  | | |____| |____| |____ 
 |_|  |_/_/ \_\ |_____/|_|  |_|______|______|______|
                                                    
                                                    
"""
        )
        while (true) {
            File(Utils.currentPwd).absolutePath.let {
                var removeLen = 0
                if (it.endsWith("."))
                    removeLen = 2
                it.removeRange(it.length - removeLen until it.length).let { workPath ->
                    Utils.currentPwd = workPath
                    print("${workPath}> ")
                }
            }
            sc.nextLine().split(" ").filter {
                it.isNotBlank()
            }.let {
                runCatching {
                    println("执行命令 -> $it")
                    commandParser(it)
                }.onFailure {
                    println("出现异常 ->　${it.localizedMessage}")
                    //it.printStackTrace()
                }
            }
        }
    }

    /**
     * 命令解析器
     * @param args List<String> 参数列表
     */
    private fun commandParser(args: List<String>) {
        val argLen = args.size
        val cmd: String = args[0]
        val param: Array<String> = args.subList(1, argLen).toTypedArray()

        runCatching {
            Cmd.valueOf(cmd.uppercase())
        }.onSuccess {
            Command(it, param).doStuff()
        }.onFailure {
            it.message?.contains("enum").let {
                println("命令错误！")
            }
        }
    }
}